import Settings from '@/settings';
import Storage from '@/utils/storage';
import LoginApi from '@/api/user/login';
import UserApi from '@/api/user';
import {
	getCurrentPage
} from '@/utils/app.js';

// console.log("【系统属性】:", Settings)

const user = {
	state: {
		// 用户认证token
		token: Storage.get(Settings.accessTokenKey),
		// 用户
		user: {},
		// 当前APP 对应的openID登录过程中存储
		openId: Storage.get(Settings.openKey),
		// 跳转的页面
		to: Storage.get(Settings.toKey),
		// 当前终端平台
		platform: Settings.platform,
		// 当前的APPID -- 有可能没有
		appId: Storage.get(Settings.appKey),
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token;
			Storage.set(Settings.accessTokenKey, token, 30 * 86400);
		},
		SET_USER: (state, value) => {
			state.user = value;
		},
		// H5 环境设置
		SET_APP_ID: (state, value) => {
			state.appId = value;
			Storage.set(Settings.appKey, value, 30 * 86400);
		},
		SET_OPEN_ID: (state, value) => {
			state.openId = value;
			console.log("## 存储 open ##", value)
			Storage.set(Settings.openKey, value, 30 * 86400);
		},
		SET_TO: (state, value) => {
			state.to = value;
			Storage.set(Settings.toKey, value);
		}
	},

	actions: {
		// 用户登录
		Login({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				console.log('data:----------------------- ',data);
				LoginApi.login(data.login, data.smsCodeKey).then(response => {
					if (response.success) {
						commit('SET_TOKEN', response.obj.token);
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						resolve(response);
					} else {
						reject(response);
					}
				}).catch(reject);
			});
		},
		// 微信h5登录
		H5WxLogin({
			commit
		}, login) {
			return new Promise((resolve, reject) => {
				LoginApi.h5weixin(login).then(response => {
					console.log(response)
					if (response.success) {
						commit('SET_TOKEN', response.obj.token);
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						resolve(response.obj);
					} else if (response.obj && response.obj.openId) {
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						reject(response.obj);
					} else {
						reject(response.obj);
					}
				}).catch(reject);
			});
		},
		// 微信小程序登录
		MpWxLogin({
			commit
		}, login) {
			return new Promise((resolve, reject) => {
				console.log('login',login)
				LoginApi.mpweixin(login).then(response => {
					console.log('response',response)
					if (response.success) {
						commit('SET_TOKEN', response.obj.token);
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						resolve(response.obj);
					} else if (response.obj && response.obj.openId) {
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						reject(response.obj);
					} else {
						reject(response.obj);
					}
				}).catch(reject);
			});
		},
		// 微信小程序绑定
		MpWxBind({
			commit
		}, login) {
			return new Promise((resolve, reject) => {
				console.log('MpWxBind',login)
				LoginApi.mpbingd({
					appId:login.appId,
					code:login.code
				}).then(response => {
					console.log('response',response)
					if (response.success) {
						commit('SET_TOKEN', response.obj.token);
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						resolve(response.obj);
					} else if (response.obj && response.obj.openId) {
						commit('SET_TOKEN', response.obj.token);
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						reject(response.obj);
					} else {
						reject(response.obj);
					}
				}).catch(reject);
			});
		},
		// 微信APP登录
		AppWxLogin({
			commit
		}, login) {
			return new Promise((resolve, reject) => {
				LoginApi.appweixin(login).then(response => {
					// console.log(response)
					if (response.success) {
						commit('SET_TOKEN', response.obj.token);
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						resolve(response);
					} else if (response.obj && response.obj.openId) {
						commit('SET_APP_ID', response.obj.appId);
						commit('SET_OPEN_ID', response.obj.openId);
						reject(response);
					} else {
						reject(response);
					}
				}).catch(reject);
			});
		},
		// 获取用户
		GetUser({
			commit
		}) {
			return new Promise((resolve, reject) => {
				UserApi.get().then(response => {
					const result = response.obj;
					commit('SET_USER', result);
					resolve(response);
				}).catch(reject);
			});
		},
		// 退出登录
		Logout({
			commit
		}, data) {
			console.log('Logout');
			let page = getCurrentPage();
			return new Promise((resolve, reject) => {
				UserApi.logout().then(() => {
					commit('SET_APP_ID', '');
					commit('SET_OPEN_ID', '');
					commit('SET_TOKEN', '');
					commit('SET_USER', {});
					commit('SET_TO', page);
					resolve();
				}).catch(reject);
			});
		}
	}
};

export default user;